
using System;
using System.Data;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

using Abp.UI;
using Abp.AutoMapper;
using Abp.Extensions;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Application.Services.Dto;
using Abp.Linq.Extensions;
 
using JYJ.AiYard.StationManagement.TbPPPayParams.Dtos;
using JYJ.AiYard.StationManagement.TbPPPayParams.DomainService;
using JYJ.AiYard.StationManagement.TbPPPayParams.Authorization;
using JYJ.AiYard.StationManagement.YardReport;
using JYJ.AiYard.StationManagement.YardCommonHald;

namespace JYJ.AiYard.StationManagement.TbPPPayParams
{
    /// <summary>
    /// TbPPPayParams应用层服务的接口实现方法  
    ///</summary>
    [AbpAuthorize]
    public class TbPPPayParamsAppService : AiYardAppServiceBase, ITbPPPayParamsAppService
    {
        private readonly IRepository<TbPPPayParams, long> _entityRepository;

        private readonly ITbPPPayParamsManager _entityManager;

		/// <summary>
		/// 构造函数 
		///</summary>
		public TbPPPayParamsAppService(
		IRepository<TbPPPayParams, long> entityRepository
		, ITbPPPayParamsManager entityManager
		)
		{
			_entityRepository = entityRepository;
			_entityManager = entityManager;
		}


        /// <summary>
        /// 获取TbPPPayParams的分页列表信息
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
		[AbpAuthorize(TbPPPayParamsPermissions.Query)] 
        public async Task<PagedResultDto<TbPPPayParamsListDto>> GetPaged(GetTbPPPayParamssInput input)
		{
			List<long> StationList = new List<long>();
			tbQFilterText filterText = input.FilterText.ToObject<tbQFilterText>();
			if (filterText.StationId == null || filterText.StationId <= 0)
			{
				StationList = GetCurrenUserOrganizationAndChildrenUnit();
			}
			else
			{
				StationList.Add((long)filterText.StationId);
			}
			var query = _entityRepository.GetAll().Where(o => StationList.Contains(o.Station_ID));
			// TODO:根据传入的参数添加过滤条件
            

			var count = await query.CountAsync();

			var entityList = await query               
                    .OrderBy(input.Sorting).AsNoTracking()
					.PageBy(input)
					.ToListAsync(); 
			var entityListDtos =entityList.MapTo<List<TbPPPayParamsListDto>>();

			return new PagedResultDto<TbPPPayParamsListDto>(count,entityListDtos);
		}


		/// <summary>
		/// 通过指定id获取TbPPPayParamsListDto信息
		/// </summary>
		[AbpAuthorize(TbPPPayParamsPermissions.Query)] 
		public async Task<TbPPPayParamsListDto> GetById(EntityDto<long> input)
		{
			var entity = await _entityRepository.GetAsync(input.Id);

		    return entity.MapTo<TbPPPayParamsListDto>();
		}

		/// <summary>
		/// 获取编辑 TbPPPayParams
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[AbpAuthorize(TbPPPayParamsPermissions.Create,TbPPPayParamsPermissions.Edit)]
		public async Task<GetTbPPPayParamsForEditOutput> GetForEdit(NullableIdDto<long> input)
		{
			var output = new GetTbPPPayParamsForEditOutput();
            TbPPPayParamsEditDto editDto;

			if (input.Id.HasValue)
			{
				var entity = await _entityRepository.GetAsync(input.Id.Value);

				editDto = entity.MapTo<TbPPPayParamsEditDto>(); 
			}
			else
			{
				editDto = new TbPPPayParamsEditDto();
			}

			output.TbPPPayParams = editDto;
			return output;
		}


		/// <summary>
		/// 添加或者修改TbPPPayParams的公共方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[AbpAuthorize(TbPPPayParamsPermissions.Create,TbPPPayParamsPermissions.Edit)]
		public async Task CreateOrUpdate(CreateOrUpdateTbPPPayParamsInput input)
		{
            if (input.TbPPPayParams.Station_ID <= 0)
            {
                throw new UserFriendlyException("请选择确定的场所名称");
            }
            if (input.TbPPPayParams.Id.HasValue)
			{
				await Update(input.TbPPPayParams);
			}
			else
			{
				await Create(input.TbPPPayParams);
			}
		}


		/// <summary>
		/// 新增TbPPPayParams
		/// </summary>
		[AbpAuthorize(TbPPPayParamsPermissions.Create)]
		protected virtual async Task<TbPPPayParamsEditDto> Create(TbPPPayParamsEditDto input)
		{
            if (string.IsNullOrEmpty(input.Client_ID.ToString()))
                input.Client_ID = 0;
             
            //判断相同的客户端是否只存在一个
            var query = _entityRepository.GetAll()
                .Where(o => o.Client_ID == input.Client_ID && o.Station_ID == input.Station_ID && o.pp_Type==input.pp_Type);
            if (query.Count() > 0)
            {
                throw new UserFriendlyException("存在相同活动的客户端支付参数设置");
            }
             
            var entity=input.MapTo<TbPPPayParams>();
			

			entity = await _entityRepository.InsertAsync(entity);
			return entity.MapTo<TbPPPayParamsEditDto>();
		}

		/// <summary>
		/// 编辑TbPPPayParams
		/// </summary>
		[AbpAuthorize(TbPPPayParamsPermissions.Edit)]
		protected virtual async Task Update(TbPPPayParamsEditDto input)
		{
            if (string.IsNullOrEmpty(input.Client_ID.ToString()))
                input.Client_ID = 0;
             
            //判断相同的客户端是否只存在一个
            var query = _entityRepository.GetAll()
                .Where(o => o.Client_ID == input.Client_ID && o.Station_ID == input.Station_ID && o.pp_Type == input.pp_Type && o.Id!=input.Id);
            if (query.Count() > 0)
            {
                throw new UserFriendlyException("存在相同活动的客户端支付参数设置");
            }

            var entity = await _entityRepository.GetAsync(input.Id.Value);
			input.MapTo(entity);

			// ObjectMapper.Map(input, entity);
		    await _entityRepository.UpdateAsync(entity);
		}



		/// <summary>
		/// 删除TbPPPayParams信息的方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[AbpAuthorize(TbPPPayParamsPermissions.Delete)]
		public async Task Delete(EntityDto<long> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(input.Id);
		}



		/// <summary>
		/// 批量删除TbPPPayParams的方法
		/// </summary>
		[AbpAuthorize(TbPPPayParamsPermissions.BatchDelete)]
		public async Task BatchDelete(List<long> input)
		{
			// TODO:批量删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(s => input.Contains(s.Id));
		}


		///// <summary>
		///// 导出TbPPPayParams为excel表,等待开发。
		///// </summary>
		///// <returns></returns>
		//public async Task<FileDto> GetToExcel()
		//{
		//	var users = await UserManager.Users.ToListAsync();
		//	var userListDtos = ObjectMapper.Map<List<UserListDto>>(users);
		//	await FillRoleNames(userListDtos);
		//	return _userListExcelExporter.ExportToFile(userListDtos);
		//}

    }
}


